package fgd;


public class Spisok {
	
	Element head = null;

	public void add(Element elem){
		if(head == null)
		{
			head = elem;
		}
		else{
			Element current = head;
			while (current.next != null)
			{
				current = current.next;
		}
			current.next = elem;
		}
	}
	
	public void add_begin(Element elem)
	{
		if(head==null)
			head=elem;
		else elem.next = head;
		head = elem;
	}

	public void add_after(Element elem ,int elem1){
		if(head == null){
			head = elem;
		}
		else {
			Element cur = head;
		while(cur.next != null){
			if(cur.i == elem1){
				elem.next=cur.next;
				cur.next=elem;
				return;
			}
			cur = cur.next;
		}
		}}
	public void bubble_sort(Element elem){
		boolean swapped = true;
		int j = 0;
		if(head == null){
			head = elem;
		}
		else {
			Element cur = head;
		while(cur.next != null){
			do{
				if(j!=0){
					j=0;
					cur = head;
				}
				swapped = false;
			if(cur.i>cur.next.i){
				j++;
			int tmp = cur.i;
			cur.i = cur.next.i;
			cur.next.i = tmp;
			swapped = true;
			}}while(swapped);
			cur = cur.next;	
		}}}
	public void add_end(Element elem){
		if(head == null){
			head = elem;
		}
		else {
			Element cur = head;
			while(cur.next != null){
				cur = cur.next;
			}
			cur.next = elem;
			
		}
		}
public String toString()
{
	StringBuffer sb = new StringBuffer();
	sb.append("[ ");
	Element current = head;
	while (current != null){
		sb.append(current.i + " ");
		current=current.next;
	}
	sb.append("]");
		return sb.toString();
}
}
